package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.BankInfoModel;
import com.pengcheng.taskapplication.model.PayStatusModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.mvp.view.widget.CodeView;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class SmsCodeActivity extends BaseActivity implements IView {

    @BindView(R.id.tv_center_title)
    TextView tv_title;

    private UserPresenterV2 userPresenterV2;

    private String bindingtxsn;
    private String uid;
    private String newtoken;
    @Override
    protected int layoutResID() {
        return R.layout.activity_sms_code;
    }

    @Override
    protected void initData() {
        tv_title.setText("短信验证");
        userPresenterV2=new UserPresenterV2(this);
        uid = SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken = SPUtils.getString(context, ConstantValue.SpType.newtoken);
        bindingtxsn= getIntent().getStringExtra("bindtxsn");
        CodeView codeView=(CodeView) findViewById(R.id.code_view);
        codeView.setmInputListener(new CodeView.OnInputFinishListener() {
            @Override
            public void onFinish(String code) {
                smsVertify(newtoken,uid,code);
            }
        });
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.smsverfity)){
            //绑定成功，需要跳转  校验是否设置过密码
            getStatus(newtoken,uid);

        }


        if(actionType.equals(ConstantValue.RequestKey.getStatus)){
            PayStatusModel model=(PayStatusModel)data;
            if(model.isexistpwd.equals("0")){
                //未设置密码
                Intent intent =new Intent(context,SetPassword.class);
                startActivity(intent);
                this.finish();
            }else{
//                //判断当前状态，是否开启免密支付
//                if(model.ispwd.equals("0")){
//                    //当前状态是免密
//                    aSwitch.setChecked(true);
//
//                }else{
//                    //当前没有开启
//                    aSwitch.setChecked(false);
//                }

                Intent intent =new Intent(context,BindBankSuccess.class);
                startActivity(intent);
                this.finish();
            }

        }
    }


    //checkout 是否设置密码


    public void getStatus(String token,String uid){
        userPresenterV2.getPayStatus(token,uid,ConstantValue.RequestKey.getStatus);
    }

    @Override
    public void onFailed(String code, String msg) {

    }

    @OnClick({R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }
    public void smsVertify(String token,String uid,String smscode){
        userPresenterV2.smsverify(token,uid,smscode,bindingtxsn,ConstantValue.RequestKey.smsverfity);
    }
}
